/*
 * Copyright (c) Huawei Technologies Co., Ltd. 2022-2022. All rights reserved.
 *
 */

import { OhosLogger } from '../utils/log/ohos-logger.js';

export class SdkVersion {
  private _log: OhosLogger = OhosLogger.getLogger(SdkVersion.name);
  private readonly version: string;
  private readonly _major: number;
  private readonly _minor: number;
  private readonly _revision: number;
  private readonly _build: number;

  constructor(version: string) {
    this.version = version;
    const versionList: string[] = version.split('.');
    if (versionList.length !== 4) {
      this._log.printErrorExit('INVALID_SDK_VERSION');
    }
    this._major = parseInt(versionList[0]);
    this._minor = parseInt(versionList[1]);
    this._revision = parseInt(versionList[2]);
    this._build = parseInt(versionList[3]);
  }

  /**
   * 获取sdk component的4位小版本号
   *
   * @return {string}
   */
  public getVersion(): string {
    return this.version;
  }

  public getMajor(): number {
    return this._major;
  }

  public getMinor(): number {
    return this._minor;
  }

  public getRevision(): number {
    return this._revision;
  }

  public getBuild(): number {
    return this._build;
  }

  /**
   * 判断当前版本号是否高于传入的版本
   *
   * @param {SdkVersion} sdkVersion
   * @returns {boolean}
   */
  public isHigherThan(sdkVersion: SdkVersion): boolean {
    if (this._major !== sdkVersion.getMajor()) {
      return this._major > sdkVersion._major;
    }
    if (this._minor !== sdkVersion.getMinor()) {
      return this._minor > sdkVersion.getMinor();
    }
    if (this._revision !== sdkVersion.getRevision()) {
      return this._revision > sdkVersion.getRevision();
    }
    if (this._build !== sdkVersion.getBuild()) {
      return this._build > sdkVersion.getBuild();
    }
    return false;
  }

  /**
   * 判断当前版本号是否等于传入的版本
   *
   * @param {SdkVersion} sdkVersion
   * @returns {boolean}
   */
  public equals(sdkVersion: SdkVersion): boolean {
    return (
      this._build === sdkVersion.getBuild() &&
      this._revision === sdkVersion.getRevision() &&
      this._minor === sdkVersion.getMinor() &&
      this._major === sdkVersion.getMajor()
    );
  }
}
